package com.qst.crop.demo.controller;

import com.qst.crop.demo.common.Result;
//import com.qst.crop.demo.common.UserSessionUtils;
import com.qst.crop.demo.entity.Knowledge;
import com.qst.crop.demo.entity.Product;
import com.qst.crop.demo.entity.User;
import com.qst.crop.demo.service.KnowService;
import com.qst.crop.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.List;

@RestController
@RequestMapping("/know")
@CrossOrigin(origins = "*")
public class KnowController {

    @Autowired
    KnowService knowService;
    @Autowired
    UserService userService;

    /**
     *  添加农业知识
     * @param email
     * @param knowledge
     * @return
     */
    @RequestMapping("/add")
    public Result add(String email, Knowledge knowledge){
        System.out.println("添加农业知识");
        if(knowledge == null || knowledge.getKonwledgeId() <= 0 || !StringUtils.hasLength(knowledge.getKonwledgeTitle()) || !StringUtils.hasLength(knowledge.getAuthor())){
            return Result.fail(-1, "无效参数,起码得给我个标题和作者吧");
        }

        User userinfo = userService.getUserByEmail(email);
        if(userinfo == null){
            return Result.fail(-2, "不存在该用户");
        }

        if(userinfo.getIdentity().equals("administrator")){

            knowledge.setCreatedDate(new Date(System.currentTimeMillis()));
            if(knowService.add(knowledge) == 1){
                return Result.success(200, "添加成功");
            }else{
                return Result.fail(-4, "农业知识已经存在");
            }
        }else{
            return Result.fail(-3, "没有权限");
        }

    }

//    /**
//     *  获取全部农业知识
//     * @return
//     */
//    @RequestMapping("/getAllKnow")
//    public Result getAllProducts(){
//        System.out.println("获取全部农业知识");
//
//        List<Knowledge> knowledges = knowService.selectAll();
//
//        return Result.success(200, knowledges);
//    }

    /**
     *  根据关键字搜索相关的农业知识
     * @param searchWord
     * @return
     */
    @RequestMapping("/search")
    public Result select(String searchWord){
        System.out.println("搜索农业知识");
        if(searchWord == null){
            return Result.fail(-1, "非法请求");
        }

        List<Knowledge> knowledges = knowService.select(searchWord);
        if(knowledges.size() == 0){
            return Result.fail(-2, "未找到相关商品");
        }

        return Result.success(200, knowledges);
    }

    /**
     *  根据id修改农业知识
     * @param email
     * @param knowledge
     * @return
     */
    @RequestMapping("/update")
    public Result update(String email, Knowledge knowledge){
        System.out.println("修改农业知识");

        if(knowledge == null || knowledge.getKonwledgeId() <= 0 ) {
            return Result.fail(-1, "非法参数");
        }

        User userinfo = userService.getUserByEmail(email);
        if(userinfo == null){
            return Result.fail(-2, "不存在该用户");
        }

        if(userinfo.getIdentity().equals("administrator")){

            if(knowService.update(knowledge) == 1){
                return Result.success(200, "修改成功");
            }else{
                return Result.fail(-4, "不存在该商品");
            }
        }else{
            return Result.fail(-3, "没有权限");
        }
    }

    /**
     *
     * @param email
     * @param knowledge
     * @return
     */
    @RequestMapping("/del")
    public Result delete(String email, Knowledge knowledge){
        System.out.println("删除农业知识");

        if(knowledge == null || knowledge.getKonwledgeId() <= 0 ){
            return Result.fail(-1, "非法参数/请求");
        }

        User userinfo = userService.getUserByEmail(email);
        if(userinfo == null){
            return Result.fail(-2, "不存在该用户");
        }

        if(userinfo.getIdentity().equals("administrator")){
            knowService.delete(knowledge);
        }else{
            return Result.fail(-3, "没有权限");
        }

        return Result.success(200, "删除成功");
    }

    /**
     *  根据id获得农业知识
     * @param knowledgeId
     * @return
     */
    @RequestMapping("/getKnowById")
    public Result getProById(Long knowledgeId){
        System.out.println("根据农业知识id获得商品");
        if(knowledgeId <= 0){
            return Result.fail(-1, "非法参数");
        }

        Knowledge knowledge = knowService.getKnowById(knowledgeId);

        return Result.success(200, knowledge);
    }

}
